import {
  login,
  getAccountInfo
} from '@/api/account'
import {
  getToken,
  setToken,
  removeToken,
  getAccessToken,
  setAccessToken,
  removeAccessToken,
  getUserId,
  setUserId,
  removeUserId
} from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  accessToken: getAccessToken(),
  id: getUserId(),
  acctive: '',
  roles: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ACCESS_TOKEN: (state, accessToken) => {
    state.accessToken = accessToken
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_NAME: (state, name) => {
    state.acctive = name
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { acctive, password } = userInfo
    return new Promise((resolve, reject) => {
      login({
        acctive: acctive.trim(),
        password: password
      }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        commit('SET_ID', data.id)
        setToken(data.token)
        setUserId(data.id)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // sso login
  ssoLogin({ commit }, data) {
    const { accessToken, token, id } = data
    commit('SET_TOKEN', token)
    commit('SET_ID', id)
    commit('SET_ACCESS_TOKEN', accessToken)
    setToken(token)
    setUserId(id)
    setAccessToken(accessToken)
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getAccountInfo(state.id).then(response => {
        const { data } = response
        // console.log(data)

        if (!data) {
          reject('Verification failed, please Login again.')
        }
        const { id, acctive } = data.user
        const { role } = data
        const roles = []
        roles.push(role.name)
        // roles must be a non-empty array
        if (!roles || roles.length <= 0) {
          reject('getInfo: roles must be a non-null array!')
        }
        // console.log(roles)
        commit('SET_ROLES', roles)
        commit('SET_ID', id)
        commit('SET_NAME', acctive)
        resolve(data)
      }).catch(error => {
        // console.log(error)
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken() // must remove  token  first
      removeAccessToken()
      removeUserId()
      resetRouter()
      commit('SET_TOKEN', '')
      commit('SET_ACCESS_TOKEN', '')
      commit('SET_ID', '')
      commit('SET_NAME', '')
      commit('SET_ROLES', [])
      // commit('RESET_STATE')
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      removeAccessToken()
      removeUserId()
      commit('SET_TOKEN', '')
      commit('SET_ACCESS_TOKEN', '')
      commit('SET_ID', '')
      commit('SET_NAME', '')
      commit('SET_ROLES', [])
      // commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

